#!/usr/bin/python
################################################################################
# 
#  Title:         Data Generation script for Simmode
#
#  Author:        LTE eNB Integration & Verification 
#
#  Description:   Python script that handles the data generation for simmode
#  
#  Copyright 2008-2012 Nokia Siemens Networks Oyj
#
################################################################################


#################################################
# Import libraries and Modules
#################################################
# Python Libraries and Modules
import csv
import os
import sys
import getopt

#############################################
# create_template_datagen4simmode.py usage
#############################################
def usage():
   print ""
   print " Usage: create_template_datagen4simmode.py [-h] <datagen_type>"
   print ""
   print " Input arguments:"
   print "   datagen_type       : data generation type"
   print "                        one of following options: 'iperf_udp', 'iperf_tcp' and 'ping'"
   print " Options:"
   print "   -h                  : print this help message and exit"
   print ""
   print ""


##########################################################################################
# M A I N
##########################################################################################

#--------------------------------------------------------------------------------
# read command line arguments 
#--------------------------------------------------------------------------------
try:
    opts, args = getopt.getopt(sys.argv[1:], "hi:r:", ["help", "ue_ids=","randomSamples="])
    if len(args) == 0:
        usage()
        sys.exit()
    else:
        # dataType: 'ping', 'iperf_udp', 'iperf_tcp'
        dataType = args[0]
        if dataType != 'ping' and dataType != 'iperf_udp' and dataType != 'iperf_tcp':
            raise getopt.GetoptError("invalid input arguments: unknown dataType='%s'"%dataType)
except getopt.GetoptError, err:
    # print help information and exit:
    print str("invalid options arguments: %s"%err) # will print something like "option -a not recognized"
    usage()
    sys.exit(2)

#--------------------------------------------------------------------------------
# default parameters values
#--------------------------------------------------------------------------------
NumCells = 3
ul_time_duration = 51000
dl_time_duration = 51000

# UE settings
#UeName = "I-PHY_464_"
UeName = ""
UeIpaddr = "10.34.166.1"
NumUEs = 840
NumDRBs_perUE = 1
DRBid_startIndex = 4

# [PdnSettings]
# UE appserv
PdnNetworkPrefix = '1.7'
##PdnSubNetworkPrefix = 11
##PdnIpAddressSuffix = 1
PdnAppBasePort = 40001

# CN appserv
PeerNetworkPrefix = '11.7'
##PeerSubNetworkPrefix = 11
##PeerIpAddressSuffix = 1
PeerAppBasePort = 50001

# eNB settings
SGWipaddr="172.18.1.164"
eNBipaddr="172.18.1.64"

#--------------------------------------------------------------------------------
# parse command line arguments
#--------------------------------------------------------------------------------
for opt, optValue in opts:
    if opt in ("-h", "--help"):
        usage()
        sys.exit()
    else:
        assert False, "unhandled option '%s'"%opt
# ...

#--------------------------------------------------------------------------
# Create Data Generation Configuration Table
#--------------------------------------------------------------------------
csvFilename='%s%s_template.csv'%(UeName,dataType)
outFile = open(csvFilename, 'wb')
dataGen_ConfigTable = csv.writer(outFile, delimiter=';',quotechar='|', quoting=csv.QUOTE_MINIMAL)
dataGen_ConfigTable.writerow(["CELL",
                            "UE",
                            "DRB",
                            "UE IP Address",
                            "CN IP Address",
                            "DL Data Generation Application (ping, iperf)",
                            "DL Protocol field IP (e.g. UDP, TCP)",
                            "DL Port",
                            "DL NumberOfStreams",
                            "DL Throughput [bps] (UDP only)",
                            "DL Packet Size [bytes]",
                            "DL Time Duration [sec]",
                            "UL Data Generation Application (ping, iperf)",
                            "UL Protocol field IP (e.g. UDP, TCP)",
                            "UL Port",
                            "UL NumberOfStreams",
                            "UL Throughput [bps] (UDP only)",
                            "UL Packet Size [bytes]",
                            "UL Time Duration [sec]",
                            ])
print "creating CSV configuration file for data generation...."
print "CSV filename: %s"%csvFilename
for cellid in range(1,NumCells+1):
    PdnSubNetworkPrefix = 10 * cellid + 1
    PdnIpAddressSuffix = 1
    PeerSubNetworkPrefix = 10 * cellid + 1
    PeerIpAddressSuffix = 1
    print "  cellid=%s NumUEs=%s PdnSubNetworkPrefix=%s PeerSubNetworkPrefix=%s"%(cellid,NumUEs,PdnSubNetworkPrefix,PeerSubNetworkPrefix)
    for ueid in range(1,NumUEs+1):
        for drbid in range(0,NumDRBs_perUE):
            if ueid == 251 and drbid==0:
                PdnSubNetworkPrefix += 1
                PeerSubNetworkPrefix += 1
            elif ueid == 501 and drbid==0:
                PdnSubNetworkPrefix += 1
                PeerSubNetworkPrefix += 1
            elif ueid == 751 and drbid==0:
                PdnSubNetworkPrefix += 1
                PeerSubNetworkPrefix += 1
            if ueid>0 and ueid<=250:
                PdnIpId = PdnIpAddressSuffix + ueid - 1
                PeerIpId = PeerIpAddressSuffix + ueid - 1 
            elif ueid>=251 and ueid<=500:
                PdnIpId = PdnIpAddressSuffix + ueid - 251
                PeerIpId = PeerIpAddressSuffix + ueid - 251
            elif ueid>=501 and ueid<=750:
                PdnIpId = PdnIpAddressSuffix + ueid - 501
                PeerIpId = PeerIpAddressSuffix + ueid - 501
            elif ueid>=751 and ueid<=1000:
                PdnIpId = PdnIpAddressSuffix + ueid - 751
                PeerIpId = PeerIpAddressSuffix + ueid - 751
            else:
                assert False, "UE indexes out of range for ueid=%s !!!"%ueid
            if dataType == 'ping':
                if drbid==0:
                    dataGen_ConfigTable.writerow(["cell%d"%cellid,
                                                "UE%d%3.3d"%(cellid,ueid), 
                                                DRBid_startIndex+drbid, 
                                                "%s.%s.%s"%(PdnNetworkPrefix,PdnSubNetworkPrefix,PdnIpId),
                                                "%s.%s.%s"%(PeerNetworkPrefix,PeerSubNetworkPrefix,PeerIpId),
                                                "ping","-", "-", "-", "-", "-", "%s"%dl_time_duration,
                                                "ping", "-", "-", "-", "-", "-", "%s"%dl_time_duration,
                                                ])           
            elif dataType == 'iperf_udp':
                dataGen_ConfigTable.writerow(["cell%d"%cellid,
                                            "UE%d%3.3d"%(cellid,ueid), 
                                            DRBid_startIndex+drbid, 
                                            "%s.%s.%s"%(PdnNetworkPrefix,PdnSubNetworkPrefix,PdnIpId),
                                            "%s.%s.%s"%(PeerNetworkPrefix,PeerSubNetworkPrefix,PeerIpId),
                                            "iperf",
                                            "UDP",
                                            PdnAppBasePort+1000*cellid+10*drbid+ueid,
                                            1,
                                            "10K",
                                            "1470",
                                            "%s"%dl_time_duration,
                                            "iperf",
                                            "UDP",
                                            PeerAppBasePort+1000*cellid+10*drbid+ueid,
                                            1,
                                            "10K",
                                            "1470",
                                            "%s"%ul_time_duration,
                                            ])
            elif dataType == 'iperf_tcp':
                dataGen_ConfigTable.writerow(["cell%d"%cellid,
                                            "UE%d%3.3d"%(cellid,ueid), 
                                            DRBid_startIndex+drbid, 
                                            "%s.%s.%s"%(PdnNetworkPrefix,PdnSubNetworkPrefix,PdnIpId),
                                            "%s.%s.%s"%(PeerNetworkPrefix,PeerSubNetworkPrefix,PeerIpId),
                                            "iperf",
                                            "TCP",
                                            PdnAppBasePort+1000*cellid+10*drbid+ueid,
                                            1,
                                            "-",
                                            "1470",
                                            "%s"%dl_time_duration,
                                            "iperf",
                                            "TCP",
                                            PeerAppBasePort+1000*cellid+10*drbid+ueid,
                                            1,
                                            "-",
                                            "1470",
                                            "%s"%ul_time_duration,
                                            ])
            else:
                assert False, 'dataType %s not handled!!'%dataType
print "done!"
outFile.close()

# eof
